﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sausage.Web.Code;
using Sausage.Web.Domain;
using Sausage.Web.Models;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.Extensions;
using Sausage.Common;

namespace Sausage.Web.Controllers
{
    /// <summary>
    /// 账户信息控制器
    /// </summary>
    /// <remarks></remarks>
    public class AccountController : Controller
    {
        /// <summary>
        /// 默认动作。
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [RequireAuthorization]
        [HandleError(Order = 3, ExceptionType = typeof(UnauthenticatedException), View = "Unauthenticated")]
        [HandleError(Order = 2, ExceptionType = typeof(UnauthorizedException), View = "Unauthorized")]
        [HandleError(Order = 1, ExceptionType = typeof(Exception), View = "Exception")]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 查询账户信息列表
        /// </summary>
        /// <param name="query">查询条件模型。</param>
        /// <returns>查询的账户信息列表</returns>
        /// <remarks></remarks>
        [HttpGet]
        [JsonHandleError]
        [PermissionCheckAttribute("SystemAdmin|AccountAdmin")]
        public JsonResult Select(JosnPagingQueryModel query)
        {
            var result = UserHelper.QueryUser(query);
            return new JsonNetResult()
            {
                Data = new JsonResultViewModel { total = result.Total, data = result.Data, success = true },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                ContentEncoding = null,
                ContentType = null
            };
        }

        /// <summary>
        /// 删除账户
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("SystemAdmin|AccountAdmin")]
        public JsonResult Delete(AccountListRow data)
        {

            var user = UserHelper.GetUserByID(data.UserID);
            if (user == null)
                return Json(new JsonResultViewModel { success = true, message = "用户删除成功！" }, JsonRequestBehavior.AllowGet);

            if (user.IsBuildin)
                return Json(new JsonResultViewModel { success = false, message = "用户删除失败！内置账号不可删除！" }, JsonRequestBehavior.AllowGet);

            //执行删除操作
            UserHelper.DeleteUser(data.UserID);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "用户删除成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("SystemAdmin|AccountAdmin")]
        public JsonResult ResetPassword(AccountListRow data)
        {
            var user = UserHelper.GetUserByID(data.UserID);
            if (user == null)
                return Json(new JsonResultViewModel { success = false, data = null, message = "不存在此用户数据！" }, JsonRequestBehavior.AllowGet);

            //重置密码操作
            UserHelper.ResetPassword(data.UserID);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "重置密码成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 创建账户
        /// </summary>
        /// <returns>要新增的账号信息。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("SystemAdmin|AccountAdmin")]
        public JsonResult Create(AccountDetail data)
        {
            if (UserHelper.GetUserByLoginName(data.LoginName) != null)
                return Json(new JsonResultViewModel { success = false, message = "已有同登录名用户，请更换用户登录名！" }, JsonRequestBehavior.AllowGet);

            //Create user
            UserHelper.CreateUser(data);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "添加用户成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新账户
        /// </summary>
        /// <param name="data">要更新的账号信息。</param>
        /// <returns></returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("SystemAdmin|AccountAdmin")]
        public JsonResult Update(AccountDetail data)
        {
            //更新用户信息
            UserHelper.UpdateUser(data);

            //返回结果
            return Json(new JsonResultViewModel { success = true, message = "更新用户成功！" }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获得指定账户的详细信息。
        /// </summary>
        /// <param name="query">账户编号。</param>
        /// <returns>指定账户的详细信息。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("SystemAdmin|AccountAdmin")]
        public JsonResult Details(Guid id)
        {
            var user = UserHelper.GetUserByID(id);
            if (user == null)
                return Json(new JsonResultViewModel { success = false, data = null, message = "不存在此用户数据！" }, JsonRequestBehavior.AllowGet);


            AccountDetail data = new AccountDetail()
            {
                UserID = user.UserID,
                OrganizationID = user.OrganizationID,
                Roles = user.UserRole.Select(item => item.RoleID).ToArray(),
                UserName = user.UserName,
                LoginName = user.LoginName,
                IsBuildin = user.IsBuildin,
                IsEnabled = user.IsEnabled,
                Memo = user.Memo
            };

            return Json(new JsonResultViewModel { success = true, data = data }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新指定账户的延期提交设定。
        /// </summary>
        /// <param name="data">账户的延期提交设定。</param>
        /// <returns>执行结果。</returns>
        /// <remarks></remarks>
        [HttpPost]
        [JsonHandleError]
        [PermissionCheckAttribute("SystemAdmin|AccountAdmin")]
        public JsonResult UpdateDelaySubmitSetting(AccountDelaySubmitSetting data)
        {
            UserHelper.UpdateDelaySubmitSetting(data);

            return Json(new JsonResultViewModel { success = true }, JsonRequestBehavior.AllowGet);
        }
    }
}
